package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.exception.base.BaseException;
import com.runvp.common.utils.DateUtils;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizDevice;
import com.runvp.service.biz.domain.BizIotCardRenew;
import com.runvp.service.biz.service.IBizDeviceService;
import com.runvp.service.biz.service.IBizIotCardRenewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 物联网卡充值记录Controller
 * 
 * @author runvp
 * @date 2023-04-08
 */
@Controller
@RequestMapping("/biz/renew")
public class BizIotCardRenewController extends BaseController {
    private String prefix = "biz/renew";

    @Autowired
    private IBizDeviceService bizDeviceService;
    @Autowired
    private IBizIotCardRenewService bizIotCardRenewService;

    @RequiresPermissions("biz:renew:view")
    @GetMapping()
    public String renew(ModelMap mmap,Long deviceId) {
        mmap.put("deviceId", deviceId);
        return prefix + "/renew";
    }

    /**
     * 查询物联网卡充值记录列表
     */
    @RequiresPermissions("biz:renew:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizIotCardRenew bizIotCardRenew) {
        startPage();
        List<BizIotCardRenew> list = bizIotCardRenewService.selectBizIotCardRenewList(bizIotCardRenew);
        return getDataTable(list);
    }

    /**
     * 导出物联网卡充值记录列表
     */
    @RequiresPermissions("biz:renew:export")
    @Log(title = "物联网卡充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizIotCardRenew bizIotCardRenew) {
        List<BizIotCardRenew> list = bizIotCardRenewService.selectBizIotCardRenewList(bizIotCardRenew);
        ExcelUtil<BizIotCardRenew> util = new ExcelUtil<BizIotCardRenew>(BizIotCardRenew.class);
        return util.exportExcel(list, "物联网卡充值记录数据");
    }
    /**
     * 导出物联网卡充值记录列表
     */
    @GetMapping("/calcExpire")
    @ResponseBody
    public AjaxResult calcExpire(Integer effectiveDuration) {
        return AjaxResult.success("计算成功",DateUtils.addYears(new Date(),effectiveDuration));
    }
    /**
     * 新增物联网卡充值记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmap,Long deviceId) {
        mmap.put("deviceId", deviceId);
        return prefix + "/add";
    }

    /**
     * 新增保存物联网卡充值记录
     */
    @ParamValid
    @RequiresPermissions("biz:renew:add")
    @Log(title = "物联网卡充值记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizIotCardRenew bizIotCardRenew) {
        BizDevice device = bizDeviceService.selectBizDeviceById(bizIotCardRenew.getDeviceId());
        if(null == device){
            throw new BaseException("设备不存在");
        }
        bizIotCardRenew.setDeviceCode(device.getDeviceCode());
        bizIotCardRenew.setTenantId(device.getTenantId());
        if(null == bizIotCardRenew.getExpireTime()){
            bizIotCardRenew.setExpireTime(DateUtils.addYears(new Date(),bizIotCardRenew.getEffectiveDuration()));
        }
        return toAjax(bizIotCardRenewService.insertBizIotCardRenew(bizIotCardRenew));
    }

    /**
     * 修改物联网卡充值记录
     */
    @RequiresPermissions("biz:renew:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizIotCardRenew bizIotCardRenew = bizIotCardRenewService.selectBizIotCardRenewById(id);
        mmap.put("bizIotCardRenew", bizIotCardRenew);
        return prefix + "/edit";
    }
    /**
     * 物联网卡充值记录详情
     */
    @RequiresPermissions("biz:renew:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        BizIotCardRenew bizIotCardRenew = bizIotCardRenewService.selectBizIotCardRenewById(id);
        mmap.put("bizIotCardRenew", bizIotCardRenew);
        return prefix + "/detail";
    }
    /**
     * 修改保存物联网卡充值记录
     */
    @ParamValid
    @RequiresPermissions("biz:renew:edit")
    @Log(title = "物联网卡充值记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizIotCardRenew bizIotCardRenew) {
        return toAjax(bizIotCardRenewService.updateBizIotCardRenew(bizIotCardRenew));
    }

    /**
     * 删除物联网卡充值记录
     */
    @RequiresPermissions("biz:renew:remove")
    @Log(title = "物联网卡充值记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizIotCardRenewService.deleteBizIotCardRenewByIds(ids));
    }
}
